/*
 * $Id: JulianDateStamp.java 30 2006-06-08 13:26:40Z wjx $
 */
package com.someok.utils.datetime;

import java.math.BigDecimal;

/**
 * Julian Date stamp, for high precision calculations. Julian date is a real
 * number and it basicly consist of two parts: integer and fraction. Integer
 * part carries date information, fraction carries time information.
 * <p>
 *
 * For calculations that will have time precision of 1e-3 seconds, both
 * fraction and integer part must have enough numerics in it. The problem is
 * that integer part is big and, on the other hand fractional is small, and
 * since final julian date is a sum of this two values, some fraction
 * numerals may be lost. Therefore, for higher precision both
 * fractional and intger part of julian date real number has to be
 * preserved.
 *
 * <p><a href="JulianDateStamp.java.html"><i>View Source</i></a></p> *
 * @see TimeUtil
 * @version $Revision: 30 $ $Date: 2006-06-08 21:26:40 +0800 (星期四, 08 六月 2006) $
 */
public class JulianDateStamp implements java.io.Serializable {

	private static final long serialVersionUID = -8384731346964792059L;


	/**
	 * Integer part of the Julian Date number.
	 */
	public int integer;


	/**
	 * Fraction part of the Julian Date number.
	 */
	public double fraction;


	/**
	 * Default constructor.
	 */
	public JulianDateStamp() {
	}

	/**
	 * Creates JuliandDateStamp from a <code>double</code>.
	 *
	 * @param jd     julian date
	 */
	public JulianDateStamp(double jd) {
		integer = (int)jd;
		fraction = jd - (double)integer;
	}

	/**
	 * Creates JuliandDateStamp from both integer and fractional part.
	 *
	 * @param i      integer part
	 * @param d      fractional part
	 */
	public JulianDateStamp(int i, double d) {
		integer = i;
		fraction = d;
	}

	/**
	 * Creates JuliandDateStamp from <code>BigDecimal</code>.
	 *
	 * @param bd     julian date
	 */
	public JulianDateStamp(BigDecimal bd) {
		double d = bd.doubleValue();
		integer = (int) d;
		bd = bd.subtract(new BigDecimal((double) integer));
		fraction = bd.doubleValue();
	}


	/**
	 * Returns <code>double</code> value of julian date number.
	 * <b>CAUTION</b>: double values may not be suit for precision math.
	 *
	 * @return julian date as double
	 */
	public double doubleValue() {
		return (double)integer + fraction;
	}

	/**
	 * Returns <code>BigDecimal</code> value of julian date number.
	 *
	 * @return julian date as double
	 */
	public BigDecimal toBigDecimal() {
		BigDecimal bd = new BigDecimal((double) integer);
		return bd.add(new BigDecimal(fraction));
	}

	/**
	 * Simple String conversion.
	 *
	 * @return julian integer as string
	 */
	public String toString() {
		String s = "" + fraction;
		int i = s.indexOf(".");
		s = s.substring(i + 1);
		return integer + "." + s;
	}
}
